import { loginAPI, getProfileAPI } from '@/apis/user'
import { setToken, getToken, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state() {
    return {
      token: getToken() || '',
      profile: {},
      userRouter: [], // 重组路由的属性
      userName: ''
    }
  },
  mutations: {
    setStoreToken(state, newToken) {
      state.token = newToken
      setToken(newToken)
    },
    delToken(state) {
      state.token = ''
      removeToken()
    },
    setProfile(state, profile) {
      state.profile = profile
    },
    delProfile(state) {
      state.profile = {}
    },
    setUserRouter(state, data) {
      state.userRouter = data
    },
    setUserName(state, data) {
      state.userName = data
    }
  },
  actions: {
    // 登录接口
    async loginAction(state, { username, password }) {
      const res = await loginAPI({ username, password })
      state.commit('setStoreToken', res.data.token)
      state.commit('setUserName', username)
    },
    async getProfile(state) {
      const permission = await getProfileAPI()
      state.commit('setProfile', permission.data)
      return permission
    }
  }
}
